<?php

/**
* 短信管理-短信
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Sms  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 短信表列表
	*/
	public function index( $begin_time = '' ,$end_time = '' , $p = 1 ){
	
		if(!$begin_time){
			$begin_time = date('Y-m-d',time() -  7*24*60*60);
		}
		if(!$end_time){
			$end_time = date('Y-m-d',time());
		}
		
		if(strtotime( $end_time)<strtotime( $begin_time)){
			$this->error("结束时间不能小于开始时间");
		}
		$where = " where a.uid = b.id";
		$where .= " AND a.create_time >= " . strtotime($begin_time) . " AND a.create_time < " . (strtotime($end_time)+(24*60*60-1));
		
		//数据分页
		$pize = 15;//每页数量
		if(0 >= $p){
			$p = 1;
		}
		$start = ($p - 1) * $pize;
		
		$sql = "select count(*) as c from sms a,user b " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		$order = " order by a.id DESC";
		//分页查询
		$sql = "select a.*,b.id as userid,b.username  from sms a,user b " . $where  .  $order . " limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . '/' . $this->_action  . " / " . $begin_time. " / " . $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("begin_time", $begin_time);
		$this->assign("end_time", $end_time);
	
		$this->display();
	}
	
	/**
	* 删除短信
	*/
	public function delete($id = 0){
		if(IS_POST){
			!$id &&	ajax_return(0, "参数有误");
			$sql = "delete from  sms where id in ($id)";
			$this->db->query($sql);
			ajax_return(1, "删除成功");
		}
	}
	
	/**
	* 短信详情
	*/
	public function detail($id=0){
		!$id &&	$this->error("参数有误");
		$sql = "select a.*,b.username from sms a,user b where a.id = $id and a.uid=b.id";
		$result = $this->db->query($sql)->row_array();
		if(! $result){
			$this->error("记录不存在");
		}
			
		$this->assign("result", $result);
		$this->display();
		
	}
	
	/**
	* 短信状态
	*/
	public function change(){
		if(IS_POST){
			//校验参数
			$id = isset($_POST["id"]) ? (int)$_POST["id"] : 0;
			if(0 >= $id){
				ajax_return(0, "非法请求");
			}

			//查询记录
			$sql = "select status from sms where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				ajax_return(0, "数据不存在");
			}
			
			//判断当前状态
			if($result["status"] == 0){
				$value = 1;
			}
			else{
				$value = 0;
			}
			
			
			//更新记录
			$sql = "update sms set status = '$value' where id = $id";
			$this->db->query($sql);
			$return["value"] = $value;
			$return["id"] = $id;
			
			//返回结果集
			ajax_return(1, "状态已更新", $return);
		}
	}
	
	/**
	* 用户信息
	*/
	public function ajax_get_user_info(){
		if (IS_POST) {
			$id = isset($_POST["id"]) ? (int)$_POST["id"] : 0;
			//校验参数
			if (0 >= $id) {
				ajax_return(0, "非法请求");
			}
			//查询记录
			$sql = "select id,username,realname,mobile,CASE WHEN `sex`='男' THEN '先生' ELSE '女士' END sex from user where id=$id";
			$result = $this->db->query($sql)->row_array();
			if($result){
				ajax_return(1, "查询成功", $result);
			}
			else{
				ajax_return(1, "用户不存在");
			}
		}
	}
	
	/**
	* 短信发送
	*/
	public function send(){
		if(IS_POST){
			$sender = 0;
			$phone = isset($_POST["phone"]) ? substr(sql_format($_POST["phone"]), 0, 11) : "";
			$uid = isset($_POST["id"]) ? substr(sql_format($_POST["id"]), 0, 11) : "";
			$type = isset($_POST["type"]) ? substr(sql_format($_POST["type"]), 0, 255) : "";
			$title = isset($_POST["title"]) ? substr(sql_format($_POST["title"]), 0, 255) : "";
			$content = isset($_POST["content"]) ? substr(sql_format($_POST["content"]), 0, 1000) : "";
			$create_time =  time();
			$expire_time =  time() + 24*60*60;
			$send_flag = 1;
			$delieve_flag = 0;
			$status = 1;
			
			if(! $uid){
				ajax_return(0, "请选择用户");
			}
			if(! $phone){
				ajax_return(0, "请选择用户");
			}
			
			if($type=="7" && !$title){
				ajax_return(0, "请输入标题");
			}
			
			if(! $content || $content == "请输入内容"){
				ajax_return(0, "请输入内容");
			}
			
			if($type=="7"){
				//站内信
				$sql = "insert into message (sender,receiver,title,content,isread,add_time) values ('$sender','$uid','$title','$content','0','" . $create_time ."')";
			} else {
				//短信
				$result = send_sms($phone,$content);
				$delieve_flag = $result['status'];
				$delieve_info = $result['msg'];
				$send_time =  time();
				$sql = "insert into sms (sender,uid,phone,content,create_time,expire_time,send_flag,send_time,delieve_flag,delieve_info,status) values ('$sender','$uid','$phone','$content','" .$create_time ."','" . $expire_time ."','$send_flag','" . $send_time ."','$delieve_flag','$delieve_info','$status')";
			}
			$this->db->query($sql);
			$res =$this->db->insert_id();
			if(! $res){
				ajax_return( 0, "添加失败" );
			} else{
				ajax_return( 1, "添加成功" );
			}
		}
	}
}
